import Vue from 'vue'
import VueRouter from 'vue-router'
//利用路由懒加载，引入login组件
const Login = () =>
    import ('@/views/login/Login.vue')
    //引入home组件 
const Home = () =>
    import ('@/views/home/Home.vue')
const Welcome = () =>
    import ('@/views/home/welcome/Welcome.vue')
const Users = () =>
    import ('@/views/home/users/users.vue')
const Rights = () =>
    import ('@/views/home/rights/Rights.vue')
const Roles = () =>
    import ('@/views/home/roles/Roles.vue')
const Cate = () =>
    import ('@/views/home/goods/Cate.vue')
const Params = () =>
    import ('@/views/home/goods/Params.vue')
const List = () =>
    import ('@/views/home/goods/List.vue')
const Add = () =>
    import ('@/views/home/goods/Add.vue')
const Order = () =>
    import ('@/views/home/order/Order.vue')
const Report = () =>
    import ('@/views/home/report/Report.vue')

Vue.use(VueRouter)

const routes = [
    // login组件路由规则
    { path: '/', redirect: '/Login' },
    { path: '/Login', component: Login },
    // home组件路由规则
    {
        name: 'Home',
        path: '/home',
        component: Home,
        redirect: '/home/welcome',
        children: [
            // 欢迎首页路由规则
            { name: 'Welcom', path: 'welcome', component: Welcome },
            // 用户列表路由规则
            { name: 'Users', path: 'users', component: Users },
            // 权限管理路由规则
            { name: 'rights', path: 'rights', component: Rights },
            // 角色列表路由规则
            { name: 'Roles', path: 'roles', component: Roles },
            // 商品分类路由规则
            { name: 'Cate', path: 'categories', component: Cate },
            // 分类参数路由规则
            { name: 'Params', path: 'params', component: Params },
            // 商品列表路由规则
            { name: 'List', path: 'goods', component: List },
            // 商品添加路由规则
            { name: 'Add', path: 'add', component: Add },
            // 订单管理路由规则
            { name: 'Order', path: 'orders', component: Order },
            // 数据统计路由规则
            { name: 'Report', path: 'reports', component: Report },
        ]
    }
]


const router = new VueRouter({
    routes
})

// 配置全局导航守卫，防止用户跳过登录页
router.beforeEach((to, from, next) => {
    // 判断是否去登录页
    if (to.path === '/Login') {
        next()
    } else {
        // 去往其他界面需要判断是否有token
        const tokenStr = sessionStorage.getItem('token')
        if (tokenStr) {
            next()
        } else {
            next('/Login')
        }
    }
})
export default router